﻿#region License
// Copyright (c) 2012 Mund Consulting Ltd
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace SharePoint.Foundation.Publishing.Fields
{    
    class ContentTypeIDControl : BaseFieldControl
    {
        //String Constants.
        const string strDefaultContentType = "SharePoint Foundation Publishing ContentTypes";
        const string strDefaultTemplateName = "ContentTypeID";
        const string strDisplayTemplateName = "ContentTypeIdDisplay";

        protected DropDownList ddlContentTypes, ddlContentTypeGroups;
        protected Label lblContentType, lblDescription;

        protected override string DefaultTemplateName
        {
            get
            {
                if (this.ControlMode == SPControlMode.Display)
                {
                    return this.DisplayTemplateName;
                }
                else
                {
                    return strDefaultTemplateName;
                }

            }
        }

        public override string DisplayTemplateName
        {
            get
            {
                return strDisplayTemplateName;
            }
            set
            {
                this.DisplayTemplateName = value;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            SPWeb curWeb = SPContext.Current.Web;
            if (this.ControlMode == SPControlMode.Display)
            {
                lblContentType = (Label)TemplateContainer.FindControl("lblContentType");
                SPFieldMultiColumnValue val = (SPFieldMultiColumnValue)this.ItemFieldValue;
                if (val != null)
                {
                    lblContentType.Text = val[1];
                }
            }
            else
            {
                ddlContentTypes = (DropDownList)TemplateContainer.FindControl("ddlCtNames");
                ddlContentTypeGroups = (DropDownList)TemplateContainer.FindControl("ddlCtGroup");
                lblDescription = (Label)TemplateContainer.FindControl("lblCtDescription");
                SPContentTypeCollection cTypeCollection = curWeb.ContentTypes;
                SPFieldMultiColumnValue val = (SPFieldMultiColumnValue)this.ItemFieldValue;
           
                
                if (val == null)
                {
                    PopulateControls(strDefaultContentType);
                }
                ddlContentTypeGroups.AutoPostBack = true;
                ddlContentTypeGroups.SelectedIndexChanged += new EventHandler(ddlcontentGroup_SelectedIndexChanged);
                ddlContentTypes.AutoPostBack = true;
                ddlContentTypes.SelectedIndexChanged += new EventHandler(ddlContentTypes_SelectedIndexChanged);
            }
        }

        public void ddlcontentGroup_SelectedIndexChanged(Object sender, EventArgs args)
        {
            ddlContentTypes.Items.Clear();
            SPWeb curWeb = SPContext.Current.Web;
            SPContentTypeCollection cTypeCollection = curWeb.ContentTypes;
            foreach (SPContentType cType in cTypeCollection)
                {
                    if (cType.Group == ddlContentTypeGroups.SelectedValue)
                        ddlContentTypes.Items.Add(new ListItem(cType.Name, cType.Id.ToString()));
                }
            
            if (ddlContentTypes.Items.Count > 0)
            {
                SPContentType cType = cTypeCollection[new SPContentTypeId(ddlContentTypes.Items[0].Value)];
                lblDescription.Text = cType.Description;
            }
        }

        public void ddlContentTypes_SelectedIndexChanged(Object sender, EventArgs args)
        {
            SPWeb curWeb = SPContext.Current.Web;
            SPContentType cType = curWeb.ContentTypes[new SPContentTypeId(ddlContentTypes.SelectedValue)];
            lblDescription.Text = cType.Description;
        }

        public override object Value
        {
            get
            {
                this.EnsureChildControls();
                SPFieldMultiColumnValue val = new SPFieldMultiColumnValue(3);
                val[0] = ddlContentTypeGroups.SelectedValue;
                val[1] = ddlContentTypes.SelectedItem.Text;
                val[2] = ddlContentTypes.SelectedValue;
                return val;
            }
            set
            {
                this.EnsureChildControls();
                SPWeb curWeb = SPContext.Current.Web;
                SPContentTypeCollection cTypeCollection = curWeb.ContentTypes;
                SPFieldMultiColumnValue val = (SPFieldMultiColumnValue)this.ItemFieldValue;
                PopulateControls(val[0]);
                ddlContentTypeGroups.SelectedValue = val[0];
                ddlContentTypes.SelectedValue = val[2];
                SPContentType cType2 = cTypeCollection[new SPContentTypeId(val[2])];
                lblDescription.Text = cType2.Description;
            }
         }

        /// <summary>
        /// It populates all the controls with relavant data.
        /// </summary>
        private void PopulateControls(string contentTypeGroup)
        {
            if (string.IsNullOrEmpty(contentTypeGroup))
            {
                contentTypeGroup = strDefaultContentType;
            }
            SPWeb curWeb = SPContext.Current.Web;
            SPContentTypeCollection cTypeCollection = curWeb.ContentTypes;
            ArrayList contentTypeList = new ArrayList();
            foreach (SPContentType contentType in curWeb.AvailableContentTypes)
            {
                contentTypeList.Add(contentType);
            }
            foreach (object lItem in contentTypeList)
            {
                SPContentType currentCType = (SPContentType)lItem;
                if (!ddlContentTypeGroups.Items.Contains(new ListItem(currentCType.Group)))
                {
                    ddlContentTypeGroups.Items.Add(currentCType.Group);
                }

            }

            //Hidden content types have been removed.
            ddlContentTypeGroups.Items.Remove("_Hidden");

            //Assumed that all content types of the group SharePoint Foundation Publishing Infrastructure are selected in the beginning.
            ddlContentTypeGroups.SelectedValue = contentTypeGroup;
            foreach (SPContentType cType in cTypeCollection)
            {
                if (cType.Group == ddlContentTypeGroups.SelectedValue)
                    ddlContentTypes.Items.Add(new ListItem(cType.Name, cType.Id.ToString()));
            }

            if (ddlContentTypes.Items.Count > 0)
            {
                SPContentType cType = cTypeCollection[new SPContentTypeId(ddlContentTypes.Items[0].Value)];
                lblDescription.Text = cType.Description;
            }         
        }
    }
}
